﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using condortravel.operaciones.business.Security;
using condortravel.operaciones.core.Security;

using System.Security.Principal;
using condortravel.operaciones.common;
using condortravel.operaciones.common.Log;
using CondorTravelOperacionesWeb.Source;
using Spring.Context;
using Spring.Context.Support;

namespace CondorTravelOperacionesWeb.Source
{
    public class PageBase: Page
    {

        
        #region Attributes

        #endregion

        #region Properties

        public string CultureName
        {
            get { return System.Threading.Thread.CurrentThread.CurrentCulture.Name; }

        }
        public  UserLoggedIn UserLogIn
        {
            get {
                return (UserLoggedIn)Session[AppStatic.SESSION_USERAUTHENTICATED];
            }
        }
        #endregion

        #region Business Object
        private SecurityServices securityService;

        public SecurityServices SecurityServices
        {
            get { return securityService; }
            set { securityService = value; }
        }
        #endregion

        #region Page's Events

        protected override void OnError(EventArgs e)
        {            
            
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInit(EventArgs e)
        {
            
            checkAccess();
            base.OnInit(e);

        }
        #endregion

        #region Private methods
        /// <summary>
        /// Check the user's access 
        /// </summary>
        private void checkAccess()
        {
            if (!this.IsUserAuthenticated())
            {
                string appCode = ConfigurationManager.AppSettings["AppCode"];

                UserLoggedIn user = new UserLoggedIn();
                user.Domain = "local";
                user.UserID = 1; //Agente 1
                user.UserName = "Entregable 1";
                //securityService.getUserLoggedIn(appCode, Request.LogonUserIdentity.Name, this.CultureName);
                if (user != null)
                {

                    Session[AppStatic.SESSION_USERAUTHENTICATED] = user;
                    Session[AppStatic.SESSION_ISUSERAUTHENTICATED] = true;
                }
                else
                {
                    Response.Redirect(this.ResolveUrl(URLDirection.URL_ERROR_NOTACCESS));
                }
            }

        }

        #endregion

        #region public methods
        public bool IsUserAuthenticated()
        {
            if (Session[AppStatic.SESSION_ISUSERAUTHENTICATED] == null)
                return false;
            return (bool)Session[AppStatic.SESSION_ISUSERAUTHENTICATED];
        }

        #endregion

    }
}
